import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.swt.widgets.Button;


public class friendlist {

	protected Shell shell = new Shell(SWT.TITLE | SWT.RESIZE);
	private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
	protected static ClientLogic logic;
    protected List list;

    
    public friendlist()
    {
    	logic = ClientLogic.getInstance();
    	createContents();
    }
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
	    	logic = ClientLogic.getInstance();
			friendlist window = new friendlist();
			window.open();
			window.shell.addDisposeListener(new DisposeListener(){
				public void widgetDisposed(DisposeEvent e) {
					try {
						logic.logout();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					System.exit(0);
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(SWT.TITLE);
		shell.setSize(330, 540);
		shell.setText("Friend's List");
		
		Label lblCurrentUserName = new Label(shell, SWT.NONE);
		lblCurrentUserName.setBounds(10, 10, 87, 17);
		lblCurrentUserName.setText("Logged in as:");
		
		Label lblFriends = new Label(shell, SWT.NONE);
		lblFriends.setBounds(10, 33, 155, 17);
		lblFriends.setText("Friends");
		
		Label lblStatus = new Label(shell, SWT.NONE);
		lblStatus.setBounds(248, 33, 70, 17);
		lblStatus.setText("Status");
		
		list = new List(shell, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
		list.setBounds(10, 56, 308, 392);
		// use list.add(item) to add items
		HashMap<String, String> temp = logic.getFriendList();
		ArrayList<String> listOfFriends = new ArrayList<String>(temp.keySet());
		while (!listOfFriends.isEmpty()) {
			String tempName = listOfFriends.get(0);
			String str = tempName;
			for (int i = 31 - tempName.length(); i > 0; i--){
			str = str + " ";
			}
			str = str + temp.get(tempName);
			list.add(str);
			listOfFriends.remove(0);
		}
		/*if (logic.getFriendList() == null) {
			list.add("null");
		}*/
		
		Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);
		
		MenuItem mntmNewSubmenu = new MenuItem(menu, SWT.CASCADE);
		mntmNewSubmenu.setText("File");
		
		Menu menu_1 = new Menu(mntmNewSubmenu);
		mntmNewSubmenu.setMenu(menu_1);
		
		MenuItem mntmLogOut = new MenuItem(menu_1, SWT.NONE);
		mntmLogOut.setText("Log Out");
		mntmLogOut.addListener(SWT.Selection, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				try {
					logic.logout();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				logic.loginwindow = new login();
				logic.loginwindow.open();
			}
		});
		
		MenuItem mntmExit = new MenuItem(menu_1, SWT.NONE);
		mntmExit.setText("Exit");
		mntmExit.addListener(SWT.Selection, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				try {
					logic.logout();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.exit(0);
			}
		});
		
		MenuItem mntmFriends = new MenuItem(menu, SWT.CASCADE);
		mntmFriends.setText("Friends");
		
		Menu menu_2 = new Menu(mntmFriends);
		mntmFriends.setMenu(menu_2);
		
		MenuItem mntmAddFriend = new MenuItem(menu_2, SWT.NONE);
		mntmAddFriend.setText("Add Friend");
		mntmAddFriend.addListener(SWT.Selection, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				new addfriend(Display.getCurrent().getActiveShell(), 0).open();
			}
		});
		
		MenuItem mntmRemoveFriend = new MenuItem(menu_2, SWT.NONE);
		mntmRemoveFriend.setText("Remove Friend");
		mntmRemoveFriend.addListener(SWT.Selection, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				new removefriend(Display.getCurrent().getActiveShell(), 0).open();
			}
		});
		
		MenuItem mntmSettings = new MenuItem(menu, SWT.CASCADE);
		mntmSettings.setText("Settings");
		
		Menu menu_3 = new Menu(mntmSettings);
		mntmSettings.setMenu(menu_3);
		
		MenuItem mntmChangePassword = new MenuItem(menu_3, SWT.NONE);
		mntmChangePassword.setText("Change Password");
		mntmChangePassword.addListener(SWT.Selection, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				new changepassword(Display.getCurrent().getActiveShell(), 0).open();
			}
		});
		
		Label lblUserNamePlace = new Label(shell, SWT.NONE);
		lblUserNamePlace.setBounds(103, 10, 215, 17);
		lblUserNamePlace.setText(logic.getUsername());
		
		Button btnChat = new Button(shell, SWT.NONE);
		btnChat.setBounds(227, 454, 91, 29);
		formToolkit.adapt(btnChat, true, true);
		btnChat.setText("Chat");
		btnChat.addListener(SWT.Selection, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				int index = list.getSelectionIndex();
				if (index >= 0)
				{
					String userID = list.getItem(index);
					userID = userID.substring(0,userID.indexOf(" "));
					logic.initializeChat(userID);
				}
			}
		});

	}
	
	public void addFriend(String Username, String Status)
	{
		String str = Username;
	    for (int i = 31 - Username.length(); i > 0; i--){
	          str = str + " ";
		}
	      str = str + Status;
	      list.add(str);
	}
		
	public void removeFriend(String Username)
    {
		 String[] items = list.getItems();
         for (int i = 0; i < items.length; i++)
         {
        	 String str = (String) items[i];
        	 if (Username.equals(str.substring(0, Username.length())))
        	 {
        		 list.remove(i);
        		 break;
        	 }
         }
	}
}
